package com.yjc.drygoods.order.web;

import com.yjc.common.utils.R;
import com.yjc.drygoods.order.service.OrderItemService;
import com.yjc.drygoods.order.vo.SpuCommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author yjc
 * @create 2022-10-02 17:05
 */
@Slf4j
@Controller
public class CommentController {

    @Autowired
    OrderItemService orderItemService;


    /**
     * 根据订单号获取到评论的需要展示的信息并封装
     *
     * @param orderSn
     * @param model
     * @return
     */
    @GetMapping("/comment.html")
    public String commontPage(
            @RequestParam("orderSn") String orderSn,
            @RequestParam("skuId") Long skuId,
            @RequestParam(value = "commentType", required = false, defaultValue = "0") Integer commentType,
            Model model) {
        log.info("订单号:{},skuId:{}", orderSn, skuId);
        SpuCommentVo spuComment = orderItemService.getCommentBaseInfo(orderSn, skuId, commentType);
        model.addAttribute("spuComment", spuComment);
        model.addAttribute("orderSn", orderSn);
        return "comment";
    }

    @ResponseBody
    @PostMapping("/save/commentInfo")
    public R saveCommentInfo(@RequestBody SpuCommentVo spuCommentVo) {
        log.info("SpuCommentVo:{}", spuCommentVo);
        try {
            orderItemService.saveCommentInfo(spuCommentVo);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return R.ok().setData("http://member.drygoods.top/orderList.html");
    }
}
